<?php
declare (strict_types = 1);

namespace app;

use app\api\helpers\Auth;
use app\api\helpers\Tree;
use app\api\helpers\v1\jwt_token;
use app\api\model\v1\GatherCompany;
use think\App;
use think\Collection;
use think\exception\ValidateException;
use think\Paginator;
use think\Validate;

/**
 * 控制器基础类
 */
abstract class BaseController
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * @var jwt_token
     * @User: 刘海龙
     * @Date: 2021/9/22
     * @Time: 16:38
     * token基类
     */
    protected $token;
    /**
     * @var null
     * @User: 刘海龙
     * @Date: 2021/9/16
     * @Time: 14:35
     * 权限控制类
     */
    protected $auth = null;

    protected $tree = null;
    protected $search = null;

    protected $is_super = null;
    protected $user = null;
    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct(App $app)
    {
        $this->app     = $app;
        $this->request = $this->app->request;
        $this->token = new jwt_token();
        $this->auth = new Auth();
        $this->tree = new Tree();
        $unitData = GatherCompany::select()->toArray();
        $this->search =  $this->tree->instance()->init($unitData, 'pid');
        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {}

    /**
     * 验证数据
     * @access protected
     * @param  array        $data     数据
     * @param  string|array $validate 验证器名或者验证规则数组
     * @param  array        $message  提示信息
     * @param  bool         $batch    是否批量验证
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate(array $data, $validate, array $message = [], bool $batch = false)
    {
        if (is_array($validate)) {
            $v = new Validate();
            $v->rule($validate);
        } else {
            if (strpos($validate, '.')) {
                // 支持场景
                [$validate, $scene] = explode('.', $validate);
            }
            $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
            $v     = new $class();
            if (!empty($scene)) {
                $v->scene($scene);
            }
        }

        $v->message($message);

        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }

        return $v->failException(true)->check($data);
    }

    /**
     *  返回状态说明:
     * 200请求成功
     * 304资源文件未做修改
     * 402用户未登录
     * 403资源文件请求被拒绝
     * 404请求的资源（网页等）不存在
     * 500业务处理逻辑错误
     * 501参数类型错误，不支持请求
     * 502服务端接收无效请求
     * @User: 刘海龙
     * @Date: 2021/12/27
     * @Time: 9:51
     * @return \think\response\Json
     *
     */
    public function response()
    {
        $args = func_get_args();
        $data = [
            'code' => 200,
            'data' => [
                'message' => '执行成功'
            ]
        ];
        foreach ($args as $arg) {
            switch ($arg) {
                case is_string($arg):
                    $data['data']['message'] = $arg;
                    break;
                case is_numeric($arg):
                    $data['code'] = $arg;
                    break;
                default:
                    if ($arg instanceof Collection || is_array($arg)) {
                        $data['data']['list'] = $arg;
                    } elseif ($arg instanceof Paginator) {
                        $data['data']['totalCount'] = $arg->total();
                        $data['data']['pageNo'] = $arg->currentPage();
                        $data['data']['recordCount'] = $arg->count();
                        $data['data']['pageSize'] = $arg->listRows();
                        $data['data']['totalPage'] = $arg->lastPage();
                        $data['data']['list'] = $arg->items();
                    } else {
                        $data['data']['content'] = $arg;
                    }
                    break;
            }
        }
        return json($data);
    }
}
